<?php

namespace app\company\model;


use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * @property int $id
 * @property string $name
 * @property string $mobile
 * @property string $address
 * @property string $logo
 * @property int $company_id
 */
class Supplier extends Base
{


    /**
     * 订单状态
     * @param $value
     * @param $data
     * @return string
     */
    public function getTypeTextAttr($value, $data): string
    {
        return match ($data['type']) {
            1 => '个人',
            2 => '货主',
            3 => '经销商',
            4 => '供应商',
            default => '未知状态',
        };
    }

    public static function onBeforeInsert(Supplier $model)
    {
        $model->company_id = getCompanyId();
    }

    public static function onAfterInsert(Supplier $model)
    {
        $param = request()->param();
        //添加用户
        $user                 = new SupplierUser();
        $param['supplier_id'] = $model->id;
        $param['company_id']  = $model->company_id;
        $user->save($param);

        //创建完成后添加一个company_user 进行分红关联
        $companyUser = CompanyUser::create([
            'user_name' => $param['user_name'],
            'password'  => md5(rand(10000, 99999) . rand(10000, 99999)),
            'real_name' => $param['real_name'],
            'mobile'    => $param['mobile'],
            'identity'  => 5
        ]);

        //添加分成配置
        $dividend                = request()->only(['investor_id', 'investor_ratio', 'service_id', 'service_ratio']);
        $dividend['company_id']  = $model->company_id;
        $dividend['supplier_id'] = $model->id;
        $dividend['partner_id']  = $companyUser->id;
        SupplierDividend::create($dividend);
    }


    /**
     *
     * @param Supplier $model
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Exception
     */
    public static function onBeforeUpdate(Supplier $model): void
    {
        $param = request()->param();
        //修改用户
        if (isset($param['mobile']) || isset($param['real_name']) || isset($param['user_name']) || isset($param['password'])) {
            unset($param['id']);
            $user = (new SupplierUser())->where('supplier_id', $model->id)->admin()->find();
            if ($param['user_name']) {
                if ((new SupplierUser())->whereNotIn('id', $user->id)->where('user_name', $param['user_name'])->find()) {
                    exception('用户名已存在');
                }
            }
            $user->save($param);
        }


        //修改分红信息
        if (isset($param['investor_id']) || isset($param['investor_ratio']) || isset($param['service_id']) || isset($param['service_ratio'])) {
            $user = (new SupplierDividend())->where('supplier_id', request()->param('id', 0))->find();
            if (isset($param['id'])) unset($param['id']);
            $user->save($param);
        }
    }

    /**
     * 删除前事件
     * @param Supplier $model
     * @return void
     */
    public static function onBeforeDelete(Supplier $model): void
    {
        $model->supplierUsers->delete();
    }


    public function scopeSuper($query, $super = 1)
    {
        $query->where('is_super', $super)->order('id desc');
    }

    /**
     * 超级管理员(获取负责人)
     * @param $query
     */
    public function scopeAdmin($query)
    {
        $query->where('is_super', 1);
    }
}